#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class PageListProvider : CY.GFive.Core.Providers.DALProvider.IPageListProvider
	{
		#region Sql
		private static readonly string SqlSelectPageList = "SELECT [Id], [PageName],[PageURI] FROM [PageList] WHERE  [Id]=@Id";

		private static readonly string SqlInsertPageList = "INSERT INTO [PageList]([PageName],[PageURI]) values(@PageName,@PageURI);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdatePageList = "UPDATE [PageList] SET [PageName] = @PageName,[PageURI] = @PageURI WHERE  [Id]=@Id";

		private static readonly string SqlDeletePageList = "DELETE [PageList] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllPageList = "SELECT [Id], [PageName],[PageURI] FROM [PageList]";
		#endregion

		#region IPageListProvider Members
		public Core.Business.PageList Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectPageList);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.PageList pageList = new Core.Business.PageList();

				if (!reader.IsDBNull(0)) pageList.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) pageList.PageName = reader.GetString(1);
				if (!reader.IsDBNull(2)) pageList.PageURI = reader.GetString(2);

				return pageList;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.PageList pageList)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@PageName", SqlDbType.NVarChar, pageList.PageName);
			sql.AddParameter("@PageURI", SqlDbType.NVarChar, pageList.PageURI);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertPageList);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) pageList.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.PageList pageList)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@PageName", SqlDbType.NVarChar, pageList.PageName);
			sql.AddParameter("@PageURI", SqlDbType.NVarChar, pageList.PageURI);
			sql.AddParameter("@Id", SqlDbType.Int, pageList.Id);
			sql.ExecuteSql(SqlUpdatePageList);
		}

		public void Delete(Core.Business.PageList pageList)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, pageList.Id);
			sql.ExecuteSql(SqlDeletePageList);
		}

		public IList<Core.Business.PageList> GetAllPageList()
		{
			IList<Core.Business.PageList> pageListlist = new List<Core.Business.PageList>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllPageList);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.PageList pageList = new Core.Business.PageList();

					if (!reader.IsDBNull(0)) pageList.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) pageList.PageName = reader.GetString(1);
					if (!reader.IsDBNull(2)) pageList.PageURI = reader.GetString(2);

					pageList.MarkOld();
					pageListlist.Add(pageList);
				}
				reader.Close();
			}
			return pageListlist;
		}
		#endregion
	}
}
